<?php
/**
 * 离职图表
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Report_Report");
apf_require_class("Bll_Hr_Property");
class Report_DimissionDetailController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $this->report_bll = new Bll_Report_Report();
        $depid = empty($params['depid']) ? 0 : intval($params['depid']);
        $start = empty($params['start']) ? 0 : intval($params['start']);
        $end = empty($params['end']) ? 0 : intval($params['end']);
        $function_id = empty($params['function_id']) ? 0 : intval($params['function_id']);
        $bu_id = empty($params['bu_id']) ? 0 : intval($params['bu_id']);
        //检测是否有部门权限
        if(!$this->_check_view_auth($depid)){
            $this->show_tips('ERROR_NO_DEPARTMENT_AUTH');
        }
        if(!$start || !$end || $start > $end){
            $this->show_tips('ERROR_PARAM');
        }
        //这里做一次权限的再分配
        if (!empty($params['bu_id'])) {
            $_where['bu_id'] = intval($params['bu_id']);
        }
        if (!empty($params['function_id'])) {
            $_where['function_id'] = intval($params['function_id']);
        }
        if (!empty($params['city_id'])) {
            //$_where['city_id'] = intval($params['city_id']);
        }
        $dep_list = $this->report_bll->get_department_by_uid($this->user_info['user_id']);
        if(!empty($dep_list)){
            foreach($dep_list as $val){
                if($val['function_id']==$params['function_id'] && $params['bu_id']==$val['bu_id'] ){
                    $depids[] = $val['department_id'];
                }
            }
            $depids[]=$depid;
            //太垃圾了...
            $depid = array_unique($depids);
        }
        $user_list = $this->report_bll->get_user_by_dep_ids($depid,$start,$end);
        $department_name = '';
        $last_data = array();
        if(!empty($user_list)){
            $uids= array();
            ////取这些人的直接领导
            $uids = array_keys($user_list);
            $hroa_bll = new Bll_Hr_OutInterface();
            $parent_list = $hroa_bll->get_relation_parent($uids);
            $parent_uids = array_values($parent_list);
            $parent_data = $this->report_bll->get_user_by_ids($parent_uids);
            $dep_name ='';
            $params['leave_type_1']=0;
            $params['leave_type_2']=0;

            $act_reason = Bll_Hr_Property::get_active_reason();
            $passive_reason = Bll_Hr_Property::get_passive_reason();
            foreach($passive_reason as $k=>$val){
                $act_reason[$k] = $val;
            }
            foreach ($user_list as $user_id=>$val){
                if($val['leaving_type']!=1 && $val['leaving_type']!=2){
                    unset($user_list[$user_id]);
                    continue;
                }
                $dep_name = $val['department_name'];
                $city_id = $val['work_city_id'];
                $bu_id = $val['bu_id'];
                //$user_list[$user_id]['parent']=$parent_data[$parent_list[$user_id]];
                $params['leave_type_'.$val['leaving_type']] +=1;

                $last_data[$parent_list[$user_id]]['parent'] = $parent_data[$parent_list[$user_id]];
                $val['reason_note'] = $val['other_reason'];
                $val['reason_info'] = empty($act_reason[$val['reason_id']]) ? '' : $act_reason[$val['reason_id']];
                if($val['reason_id']==14){
                    $val['reason_note'] = $val['other_reason'];
                }
                $last_data[$parent_list[$user_id]]['attr'][] = $val;
            }
            $bu_list = Bll_Hr_Property::get_business_list();
            $city_list = Bll_Hr_Property::get_sort_city_list();
            $department_name = $city_list[$city_id].$bu_list[$bu_id]['bu_name'].$dep_name;
        }

        //tp($last_data);
        $this->request->set_attribute('department_name', $department_name);
        $this->request->set_attribute('params', $params);
        $this->request->set_attribute('user_list', $last_data);
        return "Report_DimissionDetail";
    }
    private function _check_view_auth($depid){
        $allow_ids = $this->report_bll->get_role_dep_ids($this->user_info['user_id']);
        if(in_array($depid,$allow_ids)){
            return true;
        }
        return false;

    }
}